package com.hmall.item.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.util.ThreadLocalUtil;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 分页查询-------数据列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "5") Integer pageSize){
        Object o = ThreadLocalUtil.get();
        /*System.out.println("-----------------------------------");
        System.out.println("本次登录用户为:"+o);
        System.out.println("-----------------------------------");*/
        PageDTO<Item> pageDTO = new PageDTO<>();
        Page<Item> page1 = new Page<>(page,pageSize);
        itemService.page(page1, null);
        pageDTO.setTotal(page1.getTotal());
        pageDTO.setList(page1.getRecords());
        return pageDTO;
    }

    /**
     * id查询指定数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item selectById(@PathVariable("id") Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 新增商品
     * @param item
     * @return
     */
    @PostMapping
    public String addItem(@RequestBody Item item){
        itemService.save(item);
        // MQ
        String jsonString = JSON.toJSONString(item);
        rabbitTemplate.convertAndSend("hmall.direct","insert",jsonString);
        System.out.println("发送队列消息--->新增");

        return "新增成功啦，宝贝 OvO! kumi~";
    }

    /**
     * 修改状态  上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void UpdateStatusById(@PathVariable(value = "id")Long id,
                                 @PathVariable(value = "status")Integer status){
        Item item = selectById(id);
        item.setStatus(status);
        itemService.updateById(item);
    }

    /**
     * 修改商品信息
     * @param item
     * @return
     */
    @PutMapping
    public String updateItem(@RequestBody Item item){

        itemService.updateById(item);
        return "修改成功啦，宝贝 OvO! kumi~";
    }

    /**
     * 逻辑删除商品
     */
    @DeleteMapping("{id}")
    public void deleteById(@PathVariable(value = "id")Long id){
        Item byId = itemService.getById(id);
        byId.setStatus(3);
        // MQ
        rabbitTemplate.convertAndSend("hmall.direct","delete",id);
        itemService.updateById(byId);
        System.out.println("发送队列消息--->删除");
    }
    @PutMapping("/stock/{itemId}/{num}")
    public void reduce(@PathVariable(value = "itemId")long id,
                       @PathVariable(value = "num")Integer num){
        Item item = itemService.getById(id);
        item.setStock(item.getStock()-num);
        itemService.updateById(item);
    }

}
